package com.adz.eztest;


import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.adz.eztest.realplay.EZRealPlayActivity;
import com.adz.eztest.util.EZUtils;
import com.adz.eztest.viewholder.DeviceViewHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.videogo.constant.IntentConsts;
import com.videogo.errorlayer.ErrorInfo;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.bean.EZCameraInfo;
import com.videogo.openapi.bean.EZDeviceInfo;
import com.videogo.util.LogUtil;


import java.util.List;

public class MainActivity extends AppCompatActivity {
    EasyRecyclerView recycleview;
    private RecyclerArrayAdapter adapter;
    LinearLayoutManager linearLayoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EZOpenSDK.getInstance().setAccessToken("at.35i4sasy89tqm73t5aot9fbr04nnvw04-9d9xpmren4-1f5jg4h-d5ammjnbi");
//        Button btn= (Button) findViewById(R.id.btn_test);
//        btn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent=new Intent(MainActivity.this, CaptureActivity.class);
//                startActivity(intent);
//            }
//        });
        recycleview= (EasyRecyclerView) findViewById(R.id.recycleview);
        linearLayoutManager=new LinearLayoutManager(this);
        recycleview.setLayoutManager(linearLayoutManager);
        adapter=new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {

                return new DeviceViewHolder(parent);
            }

            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            }
        };
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                final EZDeviceInfo deviceInfo = (EZDeviceInfo) adapter.getItem(position);
                if (deviceInfo.getCameraNum() == 1 && deviceInfo.getCameraInfoList() != null && deviceInfo.getCameraInfoList().size() == 1) {
                    final EZCameraInfo cameraInfo = EZUtils.getCameraInfoFromDevice(deviceInfo, 0);
                    if (cameraInfo == null) {
                        return;
                    }
                    Intent intent = new Intent(MainActivity.this, EZRealPlayActivity.class);
                    intent.putExtra(IntentConsts.EXTRA_CAMERA_INFO, cameraInfo);
                    intent.putExtra(IntentConsts.EXTRA_DEVICE_INFO, deviceInfo);
                    startActivityForResult(intent, 100);
                }
            }
        });
        recycleview.setAdapter(adapter);
        new GetCamersInfoListTask().execute();
    }

  private class GetCamersInfoListTask extends AsyncTask<Void, Void, List<EZDeviceInfo>> {

      @Override
      protected List<EZDeviceInfo> doInBackground(Void... params) {
          try {
              List<EZDeviceInfo> result =EzvizApplication.getOpenSDK().getDeviceList(0, 20);
              return result;
          } catch (BaseException e) {
              ErrorInfo errorInfo = (ErrorInfo) e.getObject();
              return null;
          }
      }

      @Override
      protected void onPostExecute(List<EZDeviceInfo> ezDeviceInfos) {
          super.onPostExecute(ezDeviceInfos);
          adapter.addAll(ezDeviceInfos);
          adapter.notifyDataSetChanged();
      }
  }
}
